package com.nxy.web;

import com.nxy.entity.Reply;
import com.nxy.service.ReplyService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("*.rpl")
public class ReplyServlet extends BaseServlet{
    ReplyService rs = new ReplyService();

    public void addReply(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String replyMsg = req.getParameter("replyMsg");
        String id = req.getParameter("id");
        Reply r = new Reply(Integer.valueOf(id),replyMsg);
        boolean flag = rs.addReply(r);
        if(flag){
            System.out.println("回复成功");
            req.getRequestDispatcher("index.html").forward(req,resp);
        }else{
            System.out.println("回复失败");
            req.getRequestDispatcher("index.html").forward(req,resp);
        }
    }

    public void queryReplyByMid(HttpServletRequest req, HttpServletResponse resp) throws Exception{
        //String mid = req.getParameter("mid");
        //List<Reply> list = rs.queryReplyByMid(mid);
        List<Reply> list = rs.queryReply();
        req.setAttribute("rList",list);
        req.getRequestDispatcher("m_index.jsp").forward(req,resp);
    }

    public void deleteReply(HttpServletRequest req, HttpServletResponse resp) throws Exception{
        String id = req.getParameter("rid");
        boolean flag = rs.deleteReply(id);
        if(flag){
            resp.sendRedirect("queryMessage.msg");
        }else{
            req.setAttribute("msg","删除失败");
        }
    }

    public void getOldReply (HttpServletRequest req, HttpServletResponse resp) throws Exception{
        String id = req.getParameter("rid");
        Reply r = new Reply();
        r = rs.queryReplyByRid(id);
        String msg = r.getReplyMsg();
        req.setAttribute("msg",msg);
        req.setAttribute("id",id);
        req.getRequestDispatcher("r_update.jsp").forward(req,resp);
    }

    public void updateReply(HttpServletRequest req, HttpServletResponse resp) throws Exception{
        String id = req.getParameter("id");
        String msg = req.getParameter("replyMsg");
        Reply r = new Reply();
        r.setId(Integer.valueOf(id));
        r.setReplyMsg(msg);
        boolean flag = rs.updateReply(r);
        if(flag){
            System.out.println("修改成功");
            resp.sendRedirect("index.html");
        }else {
            System.out.println("修改失败");
        }
    }

}
